import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import path from "path";
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  const PROXY_TARGETS = {
    test: "xxxx",
  };

  return {
    plugins: [
      vue(),
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: false,
          }),
        ],
      }),
    ],
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            "primary-color": "#1890ff", // 定制主题颜色
          },
          javascriptEnabled: true,
        },
        scss: {
          additionalData: `@import "bootstrap/scss/bootstrap";`,
        },
      },
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    define: {
      __APP_ENV__: JSON.stringify(env.VITE_API_BASE),
    },
    build: {
      outDir: "dist",
      assetsDir: "static",
      sourcemap: false, // 关闭 sourcemap
      rollupOptions: {
        output: {
          manualChunks: {
            // 分包策略
            antd: ["ant-design-vue"],
            vendor: ["lodash", "axios"],
          },
        },
      },
    },
    // server: {
    //   host: "0.0.0.0",
    //   port: 8001,
    //   cors: true,
    //   proxy: {
    //     "/api": {
    //       target: env.VITE_API_BASE_URL || PROXY_TARGETS.test, // 默认使用测试环境  不提交.env文件到 git--->在 .gitignore 中添加   .env*.local
    //       changeOrigin: true,
    //       rewrite: (path) => path.replace(/^\/api/, ""),
    //       configure: (proxy) => {
    //         // 代理请求日志（开发调试用）
    //         proxy.on("proxyReq", (proxyReq) => {
    //           console.log(`[PROXY] => ${proxyReq.method} ${proxyReq.path}`);
    //         });
    //       },
    //     },
    //   },
    // },
  };
});
